package aivin.android_jni_cpp.jniTool;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import aivin.android_jni_cpp.R;
import aivin.android_jni_cpp.bean.Car;

/**
 * 功能描述：      <br>
 * 创建人：Aivin     <br>
 * 创建日期： 2017/10/6      <br>
 * 最后修改日期 ：   <br>
 *
 *     https://my.oschina.net/wolfcs/blog/111309
 *
 *     http://blog.csdn.net/shensky711/article/details/52806794
 *
 *     http://so.csdn.net/so/search/s.do?q=JNI&t=blog
 */

public class MyJniTools
{

    // 好像必须要用 static 修饰 ，否则会 因为创建的先后顺序导致空指针问题 ？？！！
    static  Handler handle ;

    public MyJniTools(Activity mActivity )
    {
        resultText = (TextView) mActivity.findViewById(R.id.resultTxt);

        handle = new Handler() {
            @Override
            public void handleMessage(android.os.Message msg)
            {
                switch (msg.what) {
                    case 100:
                        resultText.setText("已经完成： "+String.valueOf(msg.obj) +"%");
                        break;

                }
            }
        };


    }



    static
    {
        System.loadLibrary("AivinNative-lib");
    }

    public static String rootName = "aivin1" ;
    private TextView resultText = null;


    public void startShowProgress( )
    {

        new Thread() {
            @Override
            public void run() {
                super.run();
                startProgress() ;
            }
        }.start();
    }

    /***
     * 当前函数在c中被调用
     * @param value  从c中传来的参数
     * @param calue2  从c中传来的参数
     * @return  将结果返回给c中的代码
     */
    public int calledByNativeC(int value, int  calue2  , String msgStr) {
        Log.i("aivin",  "Java端收到传来的参数 "+value+"  "+ calue2 +"  "+msgStr);
        return   value+calue2;

    }


    public void showResultFromNativeC(int progress)
    {
        // 此处是子线程
        //boolean isMiainThread = MyUtils.isMainThread() ;
        Message msg = new Message();
        msg.what = 100;
        msg.obj = progress ;
        handle.sendMessage(msg);

    }



    public static  native String test(String userName );
    public static  native String userLogin(String userName , String userPwd);
    public static native int goToNative( int value) ;
    public static native void getJavaFile();
    public   native void startProgress();
    public native byte[] processArray(byte[] mBytes) ;
    public native Car getCarWithArgs(String carType , int price) ;
    //public  static native String checkPackageNameIsLegal(String helloworld) ;
    //public native int checkApkSingLegal() ;



    public static int getApkHashCodeByJava(Context context)
    {

        try
        {

            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(),
                            PackageManager.GET_SIGNATURES);
            Signature[] signs = packageInfo.signatures;
            Signature sign = signs[0];
            int hashcode = sign.hashCode();
            return hashcode ;
        }catch (Exception e)
        {

            return  -1 ;
        }



    }


}
